package com.swjd.jingle.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.swjd.jingle.R;
import com.swjd.jingle.adapter.MyOnPageChange;
import com.swjd.jingle.adapter.MyPagerAdapter;
import com.xuexiang.xui.widget.banner.transform.DepthTransformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class GuideActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.img_1)
    ImageView img1;
    @BindView(R.id.img_2)
    ImageView img2;
    @BindView(R.id.img_3)
    ImageView img3;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.btn_experience)
    Button btnExperience;
    private View view1, view2, view3;
    private PagerAdapter pagerAdapter;
    private List<View> views = new ArrayList<>();
    private List<ImageView> imgs = new ArrayList<>();

    @Override
    public void initialize() {
    }

    @Override
    public void initView() {
        //加载ViewPager界面数据
        view1 = LayoutInflater.from(context).inflate(R.layout.item_guide, null);
        view2 = LayoutInflater.from(context).inflate(R.layout.item_guide, null);
        view3 = LayoutInflater.from(context).inflate(R.layout.item_guide, null);
    }

    @Override
    public void initData() {
        //设置切换动画
        vp.setPageTransformer(true, new DepthTransformer());
        view1.setBackgroundResource(R.mipmap.ic_welcome1);
        view2.setBackgroundResource(R.mipmap.ic_welcome2);
        view3.setBackgroundResource(R.mipmap.ic_welcome3);
        views.add(view1);
        views.add(view2);
        views.add(view3);
        imgs.add(img1);
        imgs.add(img2);
        imgs.add(img3);
        pagerAdapter = new MyPagerAdapter(context, views);
        vp.setAdapter(pagerAdapter);
        vp.addOnPageChangeListener(new MyOnPageChange() {
            @Override
            public void onPageSelected(int i) {
                //界面翻页时更新第几页的圆点
                for (int k = 0; k < imgs.size(); k++) {
                    if (k < i + 1) {
                        imgs.get(k).setBackgroundResource(R.drawable.oval_yes);
                    } else {
                        imgs.get(k).setBackgroundResource(R.drawable.oval_no);
                    }
                }
                //滑动到引导页最后时显示跳转按钮,不是时隐藏
                if (i == imgs.size() - 1) {
                    btnLogin.setVisibility(View.VISIBLE);
                    btnExperience.setVisibility(View.VISIBLE);
                } else {
                    btnLogin.setVisibility(View.INVISIBLE);
                    btnExperience.setVisibility(View.INVISIBLE);
                }
            }
        });

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_guide;
    }

    @Override
    @OnClick({R.id.btn_login, R.id.btn_experience})
    public void onClick(View v) {
        boolean isOne = getIntent().getBooleanExtra("isOne", true);
        if (isOne) {
            switch (v.getId()) {
                case R.id.btn_login:
                    startActivity(new Intent(context, LoginActivity.class));
                    break;
                case R.id.btn_experience:
                    startActivity(new Intent(context, MainActivity.class));
                    editor.putString("userInfo", "");
                    editor.commit();
                    app.setUserInfo(null);
                    break;
            }
            //将SharedPreferences的SharedPreferences改为true，只有第一次进入软件才会进入引导页
            editor.putBoolean("guide", true);
            editor.commit();
            finish();
        } else {
            editor.putBoolean("isOne", true);
            editor.commit();
            finish();
        }
    }

}
